package gov.child.dao.impl;

import gov.child.dao.AbstractDao;
import gov.child.dao.EnterpriseDeclareFormDao;
import gov.child.dao.exception.*;
import gov.child.entities.po.EEnterpriseDeclareForm;
import gov.child.entities.vo.Page;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * The type Enterprise declare form dao. --finish
 *
 * @author fzh
 * @since 2017 /9/10
 */
@Repository("enterpriseDeclareDao")
public class EnterpriseDeclareFormDaoImpl extends AbstractDao implements EnterpriseDeclareFormDao {

    @Override
    public EEnterpriseDeclareForm findById(Integer id) throws EntityQueryException {
        Session session = currentSession();
        String hql = "from EEnterpriseDeclareForm where id = :id order by createTime";
        return (EEnterpriseDeclareForm) session.createQuery(hql).setParameter("id", id).uniqueResult();
    }

    @Override
    public EEnterpriseDeclareForm findByUniformCode(String uniformCode) throws EntityQueryException {
        Session session = currentSession();
        String hql = "from EEnterpriseDeclareForm where uniformCode = :uc order by createTime";
        return (EEnterpriseDeclareForm) session.createQuery(hql).setParameter("uc", uniformCode).uniqueResult();
    }

    @Override
    public List findByDepartmentId(Integer id, Page page) {
        Session session = currentSession();
        String hql = "from EEnterpriseDeclareForm where guideId in (select id from EGuide where deptP = :deptId)";
        Query query = session.createQuery(hql).setParameter("deptId", id);
        return doPaginate(query, page);
    }

    @Override
    public List findByUserId(Integer userId, Page page) throws EntityQueryException {
        Session session = currentSession();
        String hql = "from EEnterpriseDeclareForm where userId = :uid order by createTime";
        Query query = session.createQuery(hql).setParameter("uid", userId);
        return doPaginate(query, page);
    }

    @Override
    public void save(EEnterpriseDeclareForm form) throws EntitySaveException {
        currentSession().save(form);
    }

    @Override
    public void delete(EEnterpriseDeclareForm form) throws EntityDeleteException {
        currentSession().delete(form);
    }

    @Override
    public void deleteByUniformCode(String uniformCode) throws EntityDeleteException {
        Session session = currentSession();
        String hql = "delete from EEnterpriseDeclareForm where uniformCode = :uc";
        Query query = session.createQuery(hql).setParameter("uc", uniformCode);
        if (query.executeUpdate() <= 0)
            throw new EntityDeleteException("delete the object of EEnterpriseDeclareForm failed.");
    }

    @Override
    public void update(EEnterpriseDeclareForm form) throws EntityUpdateException {
        // TODO maybe need optimization
        currentSession().update(form);
    }

    @Override
    public Long total() throws PageInitialException {
        Session session = currentSession();
        String hql = "select count (*) from EEnterpriseDeclareForm";
        return (Long) session.createQuery(hql).uniqueResult();
    }
}
